package mathgame.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
import mathgame.ctrl.ControlFactory;


public class HighscorePanel extends GamePanel implements Observer{

    private JButton jBackBtn;
    private JLabel jName1, jName2, jName3, jName4, jName5, jScore1, jScore2, jScore3, jScore4, jScore5;

    public HighscorePanel(){

        this.setLayout(null);
        controller.getHighscore().addObserver(this);
        icon = createImageIcon("../source/StartPanel2.png", "background");
        jBackBtn = new JButton();
        jBackBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jBackBtn.setText("BACK");
        JButton jShowHighscoreBtn = new JButton();
        jShowHighscoreBtn.setFont(new Font("monospaced", Font.BOLD, 20));
        jShowHighscoreBtn.setText("Show highscore");
        JLabel scoreArea = new JLabel("Highscores!");
        scoreArea.setFont(new Font("monospaced",Font.BOLD,32));
        scoreArea.setForeground(Color.black);
        JLabel scoreHeader = new JLabel("Score");
        scoreHeader.setFont(new Font("monospaced",Font.BOLD,28));
        scoreHeader.setForeground(Color.black);
        JLabel nameHeader = new JLabel("Name");
        nameHeader.setFont(new Font("monospaced",Font.BOLD,28));
        nameHeader.setForeground(Color.black);

        jName1 = new JLabel("");
        jName1.setFont(new Font("monospaced",Font.BOLD,24));
        jName1.setForeground(Color.black);
        jName2 = new JLabel("");
        jName2.setFont(new Font("monospaced",Font.BOLD,24));
        jName3 = new JLabel("");
        jName3.setFont(new Font("monospaced",Font.BOLD,24));
        jName4 = new JLabel("");
        jName4.setFont(new Font("monospaced",Font.BOLD,24));
        jName5 = new JLabel("");
        jName5.setFont(new Font("monospaced",Font.BOLD,24));
        jScore1 = new JLabel("");
        jScore1.setFont(new Font("monospaced",Font.BOLD,24));
        jScore2 = new JLabel("");
        jScore2.setFont(new Font("monospaced",Font.BOLD,24));
        jScore3 = new JLabel("");
        jScore3.setFont(new Font("monospaced",Font.BOLD,24));
        jScore4 = new JLabel("");
        jScore4.setFont(new Font("monospaced",Font.BOLD,24));
        jScore5 = new JLabel("");
        jScore5.setFont(new Font("monospaced",Font.BOLD,24));

        jBackBtn.addActionListener(actionListener);

        this.add(jExitBtn);
        this.add(jHelpBtn);
        this.add(jBackBtn);
        this.add(scoreArea);
        this.add(nameHeader);
        this.add(scoreHeader);
        this.add(jName1);
        this.add(jName2);
        this.add(jName3);
        this.add(jName4);
        this.add(jName5);
        this.add(jScore1);
        this.add(jScore2);
        this.add(jScore3);
        this.add(jScore4);
        this.add(jScore5);

        Insets insets = this.getInsets();
        int WIDTH_SMALL = 150;
        int HEIGHT_SMALL = 40;

        jBackBtn.setBounds(440 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        jExitBtn.setBounds(800 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        jHelpBtn.setBounds(620 + insets.left, 520 + insets.top, WIDTH_SMALL, HEIGHT_SMALL);
        jShowHighscoreBtn.setBounds(590 + insets.left, 200 + insets.top, 300, HEIGHT_SMALL);

        scoreArea.setBounds(insets.left+650 , insets.top, 250, 100);
        nameHeader.setBounds(insets.left+590, insets.top+80, 100, 50);
        scoreHeader.setBounds(insets.left+800, insets.top+80, 100, 50);

        jName1.setBounds(insets.left+590, insets.top+150, 100, 50);
        jScore1.setBounds(insets.left+800, insets.top+150,130,50);

        jName2.setBounds(insets.left+590, insets.top+210, 100, 50);
        jScore2.setBounds(insets.left+800, insets.top+210,130,50);

        jName3.setBounds(insets.left+590, insets.top+270, 100, 50);
        jScore3.setBounds(insets.left+800, insets.top+270,130,50);

        jName4.setBounds(insets.left+590, insets.top+330, 100, 50);
        jScore4.setBounds(insets.left+800, insets.top+330,130,50);

        jName5.setBounds(insets.left+590, insets.top+390, 100, 50);
        jScore5.setBounds(insets.left+800, insets.top+390,130,50);

        this.setOpaque(false);
    }

    protected void paintComponent(Graphics g){
        g.drawImage(icon.getImage(), 0, 0, null);
        super.paintComponent(g);
    }

    ActionListener actionListener = new ActionListener(){

        public void actionPerformed(ActionEvent e){

            if(e.getSource() == jBackBtn){
                controller.changeCard(1);
            }
        }
    };

    public void update(Observable o, Object arg) {
        if(arg == null){
            JOptionPane.showMessageDialog(null, "Error while reading highscore file");
        }
        else{
            Map<Integer, String> sortedMap = (TreeMap)arg;
            ArrayList<String> name = getName(sortedMap);
            ArrayList<Integer> score = getScore(sortedMap);

            for(int i=0;i<=5;i++){
                int n 	 = score.get(i);
                String s = name.get(i);
                if(i==0){
                    jScore1.setText(n+"");
                    jName1.setText(s);
                }else if(i==1){
                    jScore2.setText(n+"");
                    jName2.setText(s);
                }else if(i==2){
                    jScore3.setText(n+"");
                    jName3.setText(s);
                }else if(i==3){
                    jScore4.setText(n+"");
                    jName4.setText(s);
                }else if(i==4){
                    jScore5.setText(n+"");
                    jName5.setText(s);
                }
            }
        }
    }

    public ArrayList<String> getName(Map<Integer, String> map){
        ArrayList<String> al = new ArrayList<String>();
        Set<Map.Entry<Integer,String>> s = map.entrySet();
        for(Map.Entry<Integer, String> k : s){
            String n = k.getValue();
            al.add(n);
        }
        Collections.reverse(al);
        return al;
    }

    public ArrayList<Integer> getScore(Map<Integer, String> map){
        ArrayList<Integer> al = new ArrayList<Integer>();
        Set<Map.Entry<Integer,String>> s = map.entrySet();
        for(Map.Entry<Integer, String> k : s){
            int n = k.getKey();
            al.add(n);
        }
        Collections.reverse(al);
        return al;
    }
}
